home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 003 / xlisp / xlstr.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  4KB  |  203 lines

  1. /* xlstr - xlisp string builtin functions */
  2.  
  3. #include "xlisp.h"
  4.  
  5. /* external variables */
  6. extern NODE *xlstack;
  7.  
  8. /* external procedures */
  9. extern char *strcat();
  10.  
  11. /* xstrlen - length of a string */
  12. NODE *xstrlen(args)
  13.   NODE *args;
  14. {
  15.     NODE *val;
  16.     int total;
  17.  
  18.     /* initialize */
  19.     total = 0;
  20.  
  21.     /* loop over args and total */
  22.     while (args)
  23.     total += strlen(xlmatch(STR,&args)->n_str);
  24.  
  25.     /* create the value node */
  26.     val = newnode(INT);
  27.     val->n_int = total;
  28.  
  29.     /* return the total */
  30.     return (val);
  31. }
  32.  
  33. /* xstrcat - concatenate a bunch of strings */
  34. NODE *xstrcat(args)
  35.   NODE *args;
  36. {
  37.     NODE *oldstk,val,*p;
  38.     char *str;
  39.     int len;
  40.  
  41.     /* create a new stack frame */
  42.     oldstk = xlsave(&val,NULL);
  43.  
  44.     /* find the length of the new string */
  45.     for (p = args, len = 0; p; )
  46.     len += strlen(xlmatch(STR,&p)->n_str);
  47.  
  48.     /* create the result string */
  49.     val.n_ptr = newnode(STR);
  50.     val.n_ptr->n_str = str = stralloc(len);
  51.     *str = 0;
  52.  
  53.     /* combine the strings */
  54.     while (args)
  55.     strcat(str,xlmatch(STR,&args)->n_str);
  56.  
  57.     /* restore the previous stack frame */
  58.     xlstack = oldstk;
  59.  
  60.     /* return the new string */
  61.     return (val.n_ptr);
  62. }
  63.  
  64. /* xsubstr - return a substring */
  65. NODE *xsubstr(args)
  66.   NODE *args;
  67. {
  68.     NODE *oldstk,arg,src,val;
  69.     int start,forlen,srclen;
  70.     char *srcptr,*dstptr;
  71.  
  72.     /* create a new stack frame */
  73.     oldstk = xlsave(&arg,&src,&val,NULL);
  74.  
  75.     /* initialize */
  76.     arg.n_ptr = args;
  77.     
  78.     /* get string and its length */
  79.     src.n_ptr = xlmatch(STR,&arg.n_ptr);
  80.     srcptr = src.n_ptr->n_str;
  81.     srclen = strlen(srcptr);
  82.  
  83.     /* get starting pos -- must be present */
  84.     start = xlmatch(INT,&arg.n_ptr)->n_int;
  85.  
  86.     /* get length -- if not present use remainder of string */
  87.     forlen = (arg.n_ptr ? xlmatch(INT,&arg.n_ptr)->n_int : srclen);
  88.  
  89.     /* make sure there aren't any more arguments */
  90.     xllastarg(arg.n_ptr);
  91.  
  92.     /* don't take more than exists */
  93.     if (start + forlen > srclen)
  94.     forlen = srclen - start + 1;
  95.  
  96.     /* if start beyond string -- return null string */
  97.     if (start > srclen) {
  98.     start = 1;
  99.     forlen = 0; }
  100.     
  101.     /* create return node */
  102.     val.n_ptr = newnode(STR);
  103.     val.n_ptr->n_str = dstptr = stralloc(forlen);
  104.  
  105.     /* move string */
  106.     for (srcptr += start-1; forlen--; *dstptr++ = *srcptr++)
  107.     ;
  108.     *dstptr = 0;
  109.  
  110.     /* restore the previous stack frame */
  111.     xlstack = oldstk;
  112.  
  113.     /* return the substring */
  114.     return (val.n_ptr);
  115. }
  116.  
  117. /* xascii - return ascii value */
  118. NODE *xascii(args)
  119.   NODE *args;
  120. {
  121.     NODE *val;
  122.  
  123.     /* build return node */
  124.     val = newnode(INT);
  125.     val->n_int = *(xlmatch(STR,&args)->n_str);
  126.  
  127.     /* make sure there aren't any more arguments */
  128.     xllastarg(args);
  129.  
  130.     /* return the character */
  131.     return (val);
  132. }
  133.  
  134. /* xchr - convert an INT into a one character ascii string */
  135. NODE *xchr(args)
  136.   NODE *args;
  137. {
  138.     NODE *oldstk,val;
  139.     char *sptr;
  140.  
  141.     /* create a new stack frame */
  142.     oldstk = xlsave(&val,NULL);
  143.  
  144.     /* build return node */
  145.     val.n_ptr = newnode(STR);
  146.     val.n_ptr->n_str = sptr = stralloc(1);
  147.     *sptr++ = xlmatch(INT,&args)->n_int;
  148.     *sptr = 0;
  149.  
  150.     /* make sure there aren't any more arguments */
  151.     xllastarg(args);
  152.  
  153.     /* restore the previous stack frame */
  154.     xlstack = oldstk;
  155.  
  156.     /* return the new string */
  157.     return (val.n_ptr);
  158. }
  159.  
  160. /* xatoi - convert an ascii string to an integer */
  161. NODE *xatoi(args)
  162.   NODE *args;
  163. {
  164.     NODE *val;
  165.     int n;
  166.  
  167.     /* get the string and convert it */
  168.     n = atoi(xlmatch(STR,&args)->n_str);
  169.  
  170.     /* make sure there aren't any more arguments */
  171.     xllastarg(args);
  172.  
  173.     /* create the value node */
  174.     val = newnode(INT);
  175.     val->n_int = n;
  176.  
  177.     /* return the number */
  178.     return (val);
  179. }
  180.  
  181. /* xitoa - convert an integer to an ascii string */
  182. NODE *xitoa(args)
  183.   NODE *args;
  184. {
  185.     NODE *val;
  186.     char buf[20];
  187.     int n;
  188.  
  189.     /* get the integer */
  190.     n = xlmatch(INT,&args)->n_int;
  191.     xllastarg(args);
  192.  
  193.     /* convert it to ascii */
  194.     sprintf(buf,"%d",n);
  195.  
  196.     /* create the value node */
  197.     val = newnode(STR);
  198.     val->n_str = strsave(buf);
  199.  
  200.     /* return the string */
  201.     return (val);
  202. }
  203.